Skip to content

Know the length of an array initializer - #540

Merged
CatarinaGamboa merged 1 commit into
mainfrom
fix/448-array-initializer-length
Nov 23, 2026
Merged

CatarinaGamboa merged 1 commit into
mainfrom
fix/448-array-initializer-length

Conversation

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator

Fixes #448

Root cause

RefinementTypeChecker.visitCtNewArray builds the length(_) == n refinement of a new array only from its size expressions (new byte[n]). An array initializer (new byte[] {1, 2}, or {...} in a declaration) has no size expression, so the new array got no refinement at all and its length was unknown.

When the issue was filed, the argument with no refinement was wrapped as arg == true, which Z3 rejected (Sorts |byte[]| and Bool are incompatible). Since #496 (an argument with no known refinement is unconstrained), the crash is gone, but the correct call is still rejected because nothing is known about length(b).

Fix

When a CtNewArray has no size expressions, its refinement is length(_) == <number of elements>. This covers every element type, nested initializers (the outer length is the number of rows), and empty initializers (new int[] {}, int[] e = {};).

Before / after

static void put(byte[] b, @Refinement("_ >= 0 && _ <= length(b)") int n) { }
put(new byte[] {1, 2}, 2);
put(new byte[] {1, 2}, 3);
call main (e3b11c8) this PR
put(new byte[] {1, 2}, 2) Refinement Error: found n == 2 but expected n >= 0 && n <= length(b), counterexample length(b) == 0 Correct! Passed Verification.
put(new byte[] {1, 2}, 3) same error, counterexample length(b) == 0 Refinement Error: found n == 3 && length(b) == 2 but expected n >= 0 && n <= length(b)

I also checked these by hand: int[], String[], int[][] {{1}, {2, 3}} (length 2), new int[] {}, int[] e = {}, and @Refinement("length(_) == 2") int[] f = {4, 5} all pass. The same calls with a wrong length give a Refinement Error.

Test

I extended CorrectStringArrayLength with one call that passes an initializer and one that passes an empty declaration initializer, both against an exact _ == length(names) refinement. The fixture fails on main. I added no Error fixture, because main already reports an error for a wrong length (only for the wrong reason), so such a fixture would pass without the fix. The exact == refinement already catches a wrong element count. The full mvn test passes locally.

Relation to #400

#400 (.length on array expressions, spread varargs) gives a spread varargs call a fresh array with length == number of arguments. This PR handles explicit initializers, the case #400's count(new String[] { "x" }) passes through the normal argument path. The two PRs touch different methods and do not conflict.

Not changed here

new int[2][3] gets length == 3, because the loop over size expressions keeps the last one (the existing TODO only working for 1 dimension). So grid(new int[2][3], 3) against _ == length(b) is wrongly accepted. This bug is older than this PR and is not part of it.

🤖 Generated with Claude Code

An array initializer ({...} or new T[] {...}) has no size expression, so
the new array got no length refinement and a call that needs its length
was rejected. Its length is now the number of elements.

Fixes #448

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Codex review, round 1

Codex reviewed git diff origin/main...HEAD for correctness and soundness and ran each counterexample through the verifier.

Findings in this diff: none. Codex checked direct arguments, field initializers, assignments to existing variables, returns, nested initializers, explicit varargs arrays (v(new int[] {1}) against length(_) == 2 now gives Refinement Error: found length(xs) == 1 but expected length(xs) == 2) and annotation arrays such as @StateSet({...}). All of them verify when they should, and wrong lengths are rejected.

Bugs that are older than this PR (the same on main):

  1. A ternary between arrays crashes: @Refinement("length(_) == 1") int[] a = b ? new int[] {1} : new int[] {2}; gives Sorts (Array Int Int) and Bool are incompatible. I checked that b ? new int[1] : new int[1] crashes the same way, so the cause is how ternaries handle array values, not initializers.
  2. A spread varargs call crashes: v(1) against @Refinement("length(_) == 2") int... xs gives Sorts (Array Int Int) and Int are incompatible. Open PR Support .length on array expressions and spread varargs calls #400 already covers this.

Response

No new push for this round.

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator Author

Clean-code review

I audited only this PR's new code with the clean-code-principles checklist: the 7 lines in RefinementTypeChecker.visitCtNewArray and the fixture addition.

Finding Decision
RefinementTypeChecker.java:225, [core-kiss / func-small]: the initializer case is a guard clause with an early return, so the existing size-expression loop is unchanged and the method stays flat. No issue.
RefinementTypeChecker.java:228, [core-dry]: length(_) == x is now built both here and in the existing loop below, with different right-hand sides (a literal here, a fresh variable there). Declined. A shared helper would mean rewriting the existing loop to save one createEquals call. That is not worth it in a minimal fix.
RefinementTypeChecker.java:227, [name-meaningful]: length holds the element count as a String, because that is what createLit takes. Declined. It is used once on the next line, and the comment above says what it is. Renaming it would cost a CI run and change nothing.
RefinementTypeChecker.java:226, [doc-why-not-what]: the comment says why this branch exists (an initializer has no size expression) and refers to no issue number. No issue.
CorrectStringArrayLength.java, [lean tests]: one helper and two calls, both needed (an initializer used as an argument, and an empty initializer in a declaration). The fixture fails on main. No issue.

Nothing to fix, so no push.

@CatarinaGamboa
CatarinaGamboa merged commit 1b33036 into main Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash: an array initializer (new T[] {...}) passed to a parameter refined with length() gives "Sorts ... and Bool are incompatible"

1 participant